/*
 * Created by JFormDesigner on Tue Sep 07 10:24:45 CST 2021
 */

package com.minelx.woods.pc.form;

import java.beans.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.minelx.sqlstructures.common.Files;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.awt.*;
import java.util.Arrays;

import static java.util.stream.Collectors.joining;

/**
 * @author aaa
 */
public class AddingCollectionsDialog extends JDialog {

	public static void main(String[] args) {
		new AddingCollectionsDialog(null, new DefaultListModel<>()).setVisible(true);
	}

	private final DefaultListModel<String> collectionsListModel;

	public AddingCollectionsDialog(Window owner, DefaultListModel<String> collectionsListModel) {
		super(owner, "", ModalityType.APPLICATION_MODAL);
		this.collectionsListModel = collectionsListModel;
		initComponents();

		collectionsTree.setModel(collectionsTreeModel());
		collectionsList.setModel(collectionsListModel);
	}

	private void collectionsTreeValueChanged(TreeSelectionEvent e) {
		Object[] path = e.getPath().getPath();
		if (path.length != 3) {
			return;
		}
		String resultPath = Arrays.stream(path)
				.skip(1) // skip the first empty node
				.map(String::valueOf)
				.collect(joining("."));
		// add to result list
		collectionsListModel.addElement(resultPath);
	}

    private void contentPanelPropertyChange(PropertyChangeEvent e) {
        // TODO add your code here
    }

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		dialogPane = new JPanel();
		contentPanel = new JPanel();
		scrollPane1 = new JScrollPane();
		collectionsTree = new JTree();
		scrollPane2 = new JScrollPane();
		collectionsList = new JList<>();

		//======== this ========
		setTitle("\u6dfb\u52a0\u666e\u901a\u5b57\u6bb5");
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		var contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());

		//======== dialogPane ========
		{
			dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
			dialogPane.setLayout(new BorderLayout());

			//======== contentPanel ========
			{
				contentPanel.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), 5, -1));

				//======== scrollPane1 ========
				{

					//---- collectionsTree ----
					collectionsTree.addTreeSelectionListener(e -> collectionsTreeValueChanged(e));
					scrollPane1.setViewportView(collectionsTree);
				}
				contentPanel.add(scrollPane1, new GridConstraints(0, 0, 1, 1,
					GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
					GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
					GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
					null, null, null));

				//======== scrollPane2 ========
				{
					scrollPane2.setViewportView(collectionsList);
				}
				contentPanel.add(scrollPane2, new GridConstraints(0, 1, 1, 1,
					GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
					GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
					GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
					null, null, null));
			}
			dialogPane.add(contentPanel, BorderLayout.CENTER);
		}
		contentPane.add(dialogPane, BorderLayout.CENTER);
		setSize(715, 560);
		setLocationRelativeTo(getOwner());
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	private JPanel dialogPane;
	private JPanel contentPanel;
	private JScrollPane scrollPane1;
	private JTree collectionsTree;
	private JScrollPane scrollPane2;
	private JList<String> collectionsList;
	// JFormDesigner - End of variables declaration  //GEN-END:variables

	private static DefaultTreeModel collectionsTreeModel() {
		JSONArray collectionsAsJSON = JSON.parseArray(Files.toString("/mappings/field_tree.json"));
		DefaultMutableTreeNode root = new DefaultMutableTreeNode();
		for (JSONObject collection : collectionsAsJSON.toJavaList(JSONObject.class)) {
			DefaultMutableTreeNode node = new DefaultMutableTreeNode(collection.getString("label"));
			for (String child : collection.getJSONArray("children").toJavaList(String.class)) {
				node.add(new DefaultMutableTreeNode(child));
			}
			root.add(node);
		}
		return new DefaultTreeModel(root);
	}
}
